local skel = fk.CreateSkill {
  name = "lb__jizhuo",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb__jizhuo"] = "济浊",
  [":lb__jizhuo"] = "你的回合内，本回合弃牌堆中每存在一对点数和为14的牌（每张牌仅可计算一次），你到其他角色的距离-1。回合结束时，你可以将其中一对牌视为【桃】对距离1以内的角色使用。",

  ["#lb__jizhuo"] = "济浊：你可以将一对点数和为14的牌当【桃】对距离1以内的角色使用",

  ["$lb__jizhuo1"] = "去污除秽，请。",
  ["$lb__jizhuo2"] = "需要提提神吗？",
}

local getJiZhuo = function(ids)
  local list = {}
  for i, id in ipairs(ids) do
    for _, cid in ipairs(table.slice(ids, i + 1)) do
      if Fk:getCardById(id).number + Fk:getCardById(cid).number == 14 and not table.find(list, function(val)
            return table.contains(val, cid) or table.contains(val, id)
          end) then
        table.insert(list, { id, cid })
      end
    end
  end
  p(list)
  return list
end

skel:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    if player == target and player:hasSkill(skel.name) then
      local list = getJiZhuo(DIY.getCenterCards(Fk:currentRoom()))
      return #list > 0 and table.find(player.room.alive_players, function(to)
        return player:distanceTo(to) <= 1 and player:canUseTo(Fk:cloneCard("peach"), to)
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local s, ret = room:askToUseActiveSkill(player, {
      skill_name = "#lb__jizhuo-choose",
      cancelable = true,
      prompt = "#lb__jizhuo",
      extra_data = { extra_data = { ids = DIY.getCenterCards(room) } }
    })
    if s and ret and #ret.targets > 0 then
      event:setCostData(self, { tos = ret.targets, cards = ret.cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local card = Fk:cloneCard("peach")
    card:addSubcards(event:getCostData(self).cards)
    card.skillName = skel.name
    player.room:useCard {
      from = player,
      tos = event:getCostData(self).tos,
      card = card
    }
  end,
})

skel:addEffect("distance", {
  correct_func = function(self, from, to)
    if from:hasSkill(skel.name) and Fk:currentRoom():getCurrent() == from then
      return - #getJiZhuo(DIY.getCenterCards(Fk:currentRoom()))
    end
  end
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel
